# -*- coding: utf-8 -*-
# @Time : 2021/9/14 10:58
# @Author : hello
# @Email : xxx@gmail.com
# @File : clueyecz_bases.py
# @Project : crm

"""线索页面增删查改"""
from time import sleep

from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By


class ClueYeCZ():

    def __init__(self,driver):
        self.driver=driver

    # 线索界面功能属性
    tables = (By.CSS_SELECTOR, "#form1 > table")
    tbodys = (By.TAG_NAME, "tbody")
    trs = (By.TAG_NAME, "tr")
    tds = (By.TAG_NAME, "td")
    a = (By.TAG_NAME, "a")
    #单删
    check = (By.CSS_SELECTOR, '#form1 > table > tbody > tr:nth-child(1) > td:nth-child(1) > input')
    plcaozuo = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > ul > li:nth-child(1) > div > a')
    pldel = (By.ID, 'delete')
    #多删
    check2 = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[2]/td[1]/input')
    #全删
    check1=(By.ID,'check_all')
    #搜索
    sstj = (By.ID, 'field')
    bh = (By.ID, 'condition')
    ssk = (By.ID, 'search')
    ss = (By.ID, 'dosearch')


    def clue_table(self):
        """返回表格中的tr"""
        table = self.driver.find_element(*self.tables)
        tbody = table.find_element(*self.tbodys)
        trss = tbody.find_elements(*self.trs)
        return trss

    def click_check(self):
        """线索查看功能"""
        for tr in self.clue_table():
            td = tr.find_elements(*self.tds)
            if td[2]:
                td[-1].find_elements(*self.a)[0].click()
                break

    def click_edit(self):
        """线索编辑功能"""
        for tr in self.clue_table():
            td = tr.find_elements(*self.tds)
            if td[2]:
                td[-1].find_elements(*self.a)[-1].click()
                break

    def cluetocus(self):
        """线索转换功能"""
        for tr in self.clue_table():
            td = tr.find_elements(*self.tds)
            if td[2]:
                sleep(3)
                td[-1].find_elements(*self.a)[1].click()
                break

    def delclue(self):
        """删除单条线索"""
        for tr in self.clue_table():
            td = tr.find_elements(*self.tds)
            if td[1]:
                td[0].find_element(*self.check).click()
                break
        self.driver.find_element(*self.plcaozuo).click()
        self.driver.find_element(*self.pldel).click()
        sleep(3)


    def delclues(self):
        """多条删除"""
        for tr in self.clue_table():
            td = tr.find_elements(*self.tds)
            if td[0]:
                td[0].find_element(*self.check).click()
                sleep(3)
            if td[1]:
                td[0].find_element(*self.check2).click()
                sleep(3)
            break
        self.driver.find_element(*self.plcaozuo).click()
        self.driver.find_element(*self.pldel).click()

    def delcluess(self):
        """全部删除"""
        self.driver.find_element(*self.check1).click()
        self.driver.find_element(*self.plcaozuo).click()
        self.driver.find_element(*self.pldel).click()
        sleep(3)

    def prompt(self):
        alert = Alert(self.driver)
        alert.accept()

    def findclues(self,conditions,include,name):
        """线索搜索功能"""
        sstj_locator = self.driver.find_element(*self.sstj)
        Select(sstj_locator).select_by_visible_text(conditions)
        bh_locator = self.driver.find_element(*self.bh)
        Select(bh_locator).select_by_visible_text(include)
        self.driver.find_element(*self.ssk).send_keys(name)
        sleep(5)
        self.driver.find_element(*self.ss).click()


